package phoenix.web.page_objects.backend;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.testng.TestException;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.data_models.TestUserDataModel;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class EditPage extends PageBaseWebExt
{
	public static Document content;
	public String login;
	public String password;
	public String email;
	public String gender;
	public String orientation;
	public String birthday;
	public String mobile;
	public String name;
	public String surName;
	public String country;
	public String city;
	public String chatUpLine;
	public String description;

	public EditPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	private String getLogin()
	{
		return login;
	}

	private String getPassword()
	{
		return password;
	}

	private String getEmail()
	{
		return email;
	}

	private String getGender()
	{
		return gender;
	}

	private String getOrientation()
	{
		return orientation;
	}

	private String getBirthday()
	{
		return birthday;
	}

	private String getMobile()
	{
		return mobile;
	}

	private String getFirstName()
	{
		return name;
	}

	private String getLastName()
	{
		return surName;
	}

	private String getCountry()
	{
		return country;
	}

	private String getCity()
	{
		return city;
	}

	private String getChatUp()
	{
		return chatUpLine;
	}

	private String getDescription()
	{
		return description;
	}

	private String getAttrByName(String p_attrName)
	{
		return String.valueOf(content.getElementsByAttributeValue("name", p_attrName).attr("value"));
	}

	private String getSelectedValue(String p_attrName)
	{
		return String.valueOf(content.select("select[name=" + p_attrName + "] option[selected]").val());
	}

	private void syncUser(TestUserDataModel p_user)
	{
		content = getContent(p_user);

		login = getAttrByName("UserAdminForm[login]");
		password = getAttrByName("UserAdminForm[password]");
		email = getAttrByName("UserAdminForm[email]");
		gender = getSelectedValue("UserAdminForm[gender]");
		orientation = getSelectedValue("UserAdminForm[sexual_orientation]");
		birthday = getAttrByName("UserAdminForm[birthday]");
		mobile = getAttrByName("UserAdminForm[msisdn]");
		name = getAttrByName("UserAdminForm[first_name]");
		surName = getAttrByName("UserAdminForm[last_name]");
		country = getSelectedValue("UserAdminForm[country]");
		city = getAttrByName("UserAdminForm[location]");
		chatUpLine = content.getElementsByAttributeValue("name", "UserAdminForm[chat_up_line]").text();
		description = content.getElementsByAttributeValue("name", "UserAdminForm[description]").text();
	}

	public TestUserDataModel getUserModel(TestUserDataModel p_user)
	{
		syncUser(p_user);

		TestUserDataModel p_userModel = new TestUserDataModel(manager);
		p_userModel.setId(p_user.getId());
		p_userModel.setScreenname(this.getLogin());
		p_userModel.setPassword(this.getPassword());
		p_userModel.setEmail(this.getEmail());
		p_userModel.setGender(this.getGender());
		p_userModel.setSexuality(this.getOrientation());
		p_userModel.setCountry(this.getCountry());
		p_userModel.setCity(this.getCity());

		return p_userModel;
	}

	public void setUserModel(TestUserDataModel p_user)
	{
		syncUser(p_user);

		try
		{
			Element loginform = (Element) content.getElementById("edit-user-form");
			Elements inputElements = loginform.getElementsByTag("input");
			Elements selectElements = loginform.getElementsByTag("select");
			List<String> paramList = new ArrayList<String>();

			for (Element inputElement : inputElements)
			{
				String key = inputElement.attr("name");
				String value = inputElement.attr("value");

				value = key.contains("login") ? p_user.getScreenname() : value;
				value = key.contains("password") ? p_user.getPassword() : value;
				value = key.contains("email") ? p_user.getEmail() : value;
				value = key.contains("location") ? p_user.getCity() : value;

				paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
			}

			for (Element selectElement : selectElements)
			{
				String key = selectElement.attr("name");
				String value = selectElement.attr("value");

				value = key.contains("gender") ? p_user.getGender() : value;
				value = key.contains("sexual_orientation") ? p_user.getSexuality() : value;
				value = key.contains("country") ? p_user.getCountry() : value;

				paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
			}

			String postPrams = manager.httpURLConnection().buildParams(paramList);
			manager.httpURLConnection()
				.sendPost(manager.config().BACKEND_USER_EDIT + "?user_id=" + URLEncoder.encode(p_user.getId(), "UTF-8"), postPrams);
			log().info("User " + p_user.getId() + " has been synced with live");

		}
		catch (Exception p_ex)
		{
			throw new TestException("setUserModel for : " + p_user.getId() + " " + p_ex);
		}

	}

	private Document getContent(TestUserDataModel p_user)
	{
		try
		{
			if (!manager.backend().searchPage().isLogged())
			{
				throw new TestException("Unable to login after 5 attempts");
			}
			return Jsoup.parse(manager.httpURLConnection().getPageContent(manager.config().BACKEND_USER_EDIT + "?user_id=" + p_user.getId()));
		}
		catch (Exception p_ex)
		{
			throw new TestException("getContent for : " + p_user.getId() + " " + p_ex);
		}
	}

}
